home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / ccmd / cmpara.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  37.7 KB  |  1,427 lines

  1. /*
  2.  Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  the City of New York.  Permission is granted to any individual or
  4.  institution to use, copy, or redistribute this software so long as it
  5.  is not sold for profit, provided this copyright notice is retained.
  6.  
  7.  Author: Howie Kaye
  8. */
  9.  
  10. /*
  11.  * cmpara
  12.  * parse 'chunks' of text, with some nice actions defined.
  13.  *
  14.  * texti style data input.
  15.  */
  16.  
  17. /*
  18.  * This file contains the code to parse chunks of text, simlilarly to the 
  19.  * way MM parses a message on the DEC-20
  20.  *
  21.  * Accepts a char *, which is:
  22.  *  a buffer of initialized text -- so that the function can be 
  23.  *    interrupted, and then continued.
  24.  *  and a list of action characters.  These are only in place when inside 
  25.  *  the actual paragraph parse.  When an action char is typed.
  26.  * 
  27.  * This is pretty hairy.  What it does, is to change the action routines
  28.  * being used, to allow the default/user defined actions to work.  It also
  29.  * has to manage it's own set of cmd buffers and work buffers, so that
  30.  * calls to ccmd insertion routines (cmsti) do not overflow the buffer
  31.  * space which ccmd has been given (cmbufs call).  The routines here are
  32.  * meant to be called after a confirm has been given, and no reparses into
  33.  * previous data will be necessary.  It can be used to parse large chunks
  34.  * of text, in a manner similar to the TEXTI jsys under TOPS-20.
  35.  *
  36.  * In order to make ccmd appear to be in a normal state whenever we leave
  37.  * the paragraph environment, we have to remember the old environment, and
  38.  * 'context switch' the old environment in (and out again) whenever an
  39.  * action character is typed (the action routine may make asumptions about
  40.  * the ccmd environment, and try to parse something).  To do this, all
  41.  * action character force a call to the break_hndlr() routine.  This then
  42.  * saves the environment, calls the real handler, and then restores the
  43.  * environment.  It also fixes the cmd internal buffers to reflect the new
  44.  * state.   
  45.  * 
  46.  * When a user supplied (or default) action routine is called, it is
  47.  * passed the text it is acting on (This buffer is malloc'ed to the correct
  48.  * size, and should not be written to if the write will go past the bounds
  49.  * of the current buffer).  The routine is also passed two flags.  If the
  50.  * buffer is modified, then the modified flag should be set.  If the
  51.  * parse should end (eof,abort,etc), then the ret flags should be set.  In
  52.  * all cases, a pointer to the new text should be returned.
  53.  */
  54.  
  55.                     /* allocate cmd stuff here */
  56. #define PARAERR
  57.  
  58.                     /* system structures/defines */
  59. #include "ccmdlib.h"
  60. #include "cmfncs.h"
  61.                     /* incremental buffer size */
  62. #define BSIZ 1000
  63.                     /* minimum space left before buffer */
  64.                     /* expansion */
  65. #define MINSIZE 133
  66.  
  67. #define beep() cmputc(BELL,cmcsb._cmoj)
  68.  
  69. int paraparse(), paracomplete(), parahelp(); /* forward declarations */
  70.  
  71. /* 
  72.  * paragraph break mask
  73.  */
  74. static brktab parabrk = {
  75.   {                    /* all print characters */
  76.                     /* except tab and space */
  77.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  78.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  79.   },
  80.   {
  81.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  82.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  83.   }
  84. };
  85.     
  86. static brktab pipeparabrk = {
  87.   {                    /* just newline is a brk char */
  88.                     /* except tab and space */
  89.     0x00, 0x24, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  90.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  91.   },
  92.   {
  93.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,
  94.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  95.   }
  96. };
  97.  
  98. /*
  99.  * The behavior of this parser is different from the standard
  100.  * ccmd interface, so we must set up our own action table.
  101.  */
  102.  
  103. /*
  104.  * forward declare our action routines
  105.  */
  106. char *redispact(), *eofact(), *begact(), *fixact(), *quoact(), *delact(),
  107.      *wrdact(),*editact(),*dispact(),*insact(),*abortact(), *eolact(),
  108.      *autowrapact(), *autowraptab(), *autowrapspace(), *autowrapnl(),
  109.      *filteract(), *executeact();
  110.  
  111. /* 
  112.  * default paragraph actions
  113.  */
  114. para_actions def_actions[] = {
  115.   { '\002', insact },            /* ^B insert file */
  116.   { '\004', eofact },            /* ^D end of file */
  117.   { '\005', editact },            /* ^E invoke editor on this buffer */
  118.   { '\006', filteract },        /* ^F run buffer through filter */
  119.   { '\b', delact },            /* back space */
  120.   { '\t', autowraptab },        /* autowrap on tab */
  121.   { '\n', autowrapnl },            /* \n end of line wrap */
  122.   { '\r', autowrapnl },            /* CR or LF. */
  123.   { '\013', dispact },            /* ^K display whole buffer */
  124.   { '\f', redispact },            /* ^L redisplay screen */
  125.   { '\016', abortact },            /* ^N abort this insertion */
  126.   { '\020', executeact },        /* ^P execute command and insert */
  127.   { '\022', fixact },            /* ^R redisplay line */
  128.   { '\025', begact },            /* ^U delete line */
  129.   { '\026', quoact },            /* ^V quota character */
  130.   { '\027', wrdact },            /* ^W delete backwards word */
  131.   { ' ', autowrapspace },        /* SPC autowrap on space */
  132.   { '\177', delact },            /* DEL delete previous char */
  133.   { NULL, NULL },            /* end the table */
  134. };
  135.  
  136. static int (**oldact)();        /* old action table */
  137.  
  138.                     /* clean paragraph action table */
  139. static int (*(paraact[128]))() = {
  140.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  141.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  142.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  143.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  144.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  145.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  146.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  147.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  148.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  149.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  150.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  151.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  152.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  153.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  154.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  155.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL
  156. };
  157.  
  158.                     /* clean user defined action table */
  159. static char * (*(paraact1[128]))() = {
  160.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  161.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  162.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  163.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  164.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  165.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  166.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  167.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,
  168.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  169.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  170.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  171.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  172.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  173.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  174.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   
  175.     NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL,   NULL
  176. };
  177.  
  178. /*
  179.  * global state variables
  180.  */
  181.  
  182. static int eof=FALSE;
  183. static int abort = FALSE;
  184. static csb savecsb;
  185. static int *cmdbuf=NULL;
  186. static int set=FALSE;            /* first call to parse, or an */
  187. static jmp_buf ojmp;
  188.  
  189.                     /* handler structure */
  190. ftspec ft_para = { paraparse, parahelp, paracomplete, 0, ¶brk };
  191.  
  192. static int (*oerrh)();
  193.  
  194. parareset(code) int code; {
  195.   bcopy(ojmp, cmerjb, sizeof (jmp_buf)); /* put back old error handler */
  196.   null_actions();            /* restore action block */
  197.   cmact(oldact);            /* put back the original action table*/
  198.   set = FALSE;                /* uninitialized...for next time */
  199.   if (cmcsb._cmwbp)
  200.       free(cmcsb._cmwbp);
  201.   if (cmcsb._cmbfp)
  202.       free(cmcsb._cmwbp);
  203.   cmcsb = savecsb;
  204.   cmcsb._cmerh = oerrh;
  205.   return((*cmcsb._cmerh)(code));
  206. }
  207.  
  208. /*
  209.  * parse routine.
  210.  * parse until eof or abort get's set.
  211.  * returns buffer which has been typed, or NULL on abort
  212.  */
  213. PASSEDSTATIC int
  214. paraparse(text,textlen,fdbp,parselen,value)
  215. char *text;
  216. int textlen;
  217. fdb *fdbp;
  218. int *parselen;
  219. pval *value;
  220. {
  221.   int len,i;
  222.   brktab *btab;                /* break table to use */
  223.                     /* action call? */
  224.   static int flg;
  225.   static char *buf;
  226.   static char;
  227.  
  228.   if (cmcsb._cmflg2 & CM_EOF) {
  229.       cmcsb._cmflg2 &= ~CM_EOF;
  230.       eof = TRUE;
  231.   }
  232.  
  233.   if (!set) {                /* first time through, set things up */
  234.     bcopy(cmerjb, ojmp, sizeof (jmp_buf));
  235.     
  236.     savecsb = cmcsb;            /* save stae for later */
  237.     oerrh = cmcsb._cmerh;
  238.     if (cmdbuf != NULL)            /* get rid of old buffer */
  239.       free(cmdbuf);
  240.     if ((cmdbuf = (int *)calloc(1,BSIZ*sizeof(*cmdbuf))) == NULL)
  241.       return(PARAxNM);
  242.     if ((cmcsb._cmwbp = 
  243.      (char *)calloc(1,BSIZ*sizeof(*cmcsb._cmwbp))) == NULL) {
  244.       free(cmdbuf);
  245.       cmdbuf = NULL;
  246.       return(PARAxNM);
  247.     }
  248.     cmcsb._cmwbc = BSIZ;        /* set up. */
  249.     cmcsb._cmcnt = BSIZ;
  250.     cmcsb._cmptr = cmdbuf;
  251.     cmcsb._cmbfp = cmdbuf;
  252.     cmcsb._cmcur = cmdbuf;
  253.     cmcsb._cminc = 0;
  254.     cmcsb._cmflg &= ~CM_NAC;
  255.     cmcsb._cmerh = parareset;
  256.  
  257.     if (fdbp->_cmdat != NULL) {        /* insert specified starting text */
  258.       para_data *dat = (para_data *) fdbp->_cmdat;
  259.       if (dat->buf != NULL) {
  260.     int len = strlen(dat->buf);
  261.     if (len >= BSIZ) {        /* do we have enough space? */
  262.                     /* no!  get more */
  263.       if ((cmdbuf=(int *)cmrealloc(cmdbuf,(len+BSIZ)*sizeof(int)))==NULL){
  264.         return(PARAxNM);
  265.       }
  266.       cmcsb._cmcur = cmcsb._cmptr = cmdbuf + (cmcsb._cmptr - cmcsb._cmbfp);
  267.       cmcsb._cmbfp = cmdbuf;
  268.       cmcsb._cmcnt = len + BSIZ;
  269.  
  270.       if ((cmcsb._cmwbp = 
  271.            (char *)cmrealloc(cmcsb._cmwbp, 
  272.                  sizeof(*cmcsb._cmwbp)*(len+BSIZ))) == NULL){
  273.         return(PARAxNM);
  274.       }
  275.       cmcsb._cmwbc = len + BSIZ;
  276.     }
  277.     cmsti(dat->buf,CC_NEC|CC_HID|CC_QUO); /* insert text, no echoing */
  278.     for (i = 0; i < cmcsb._cminc; i++) { /* turn off the flags */
  279.       cmcsb._cmptr[i] &= ~(CC_HID|CC_NEC|CC_QUO);
  280.     }
  281.       }
  282.       null_actions();            /* zero out the action table */
  283.       if (cmcsb._cmflg & CM_ITTY) {
  284.     if (dat->actions != NULL) {    /* install user/default actions */
  285.       if (fdbp->_cmffl & PARA_DEF)
  286.         install_actions(def_actions);
  287.       install_actions(dat->actions);
  288.     }
  289.     else {
  290.       install_actions(def_actions);
  291.     }
  292.       }
  293.       else {
  294.     install_nl_action();
  295.       }
  296.     }
  297.     oldact = cmcsb._cmact;        /* save old action table */
  298.     cmact(paraact);            /* now use our own action characters */
  299.     eof = FALSE;            /* not done yet */
  300.     abort = FALSE;            /* not aborting */
  301.     set = TRUE;                /* but we are initialized */
  302.   }
  303.  
  304.   if (cmcsb._cmcnt < MINSIZE) {        /* running out of buffer space? */
  305.                     /* then get more space */
  306.     if ((cmdbuf=(int *)cmrealloc(cmdbuf,(cmcsb._cmwbc + BSIZ)
  307.                  *sizeof(int)))==NULL){
  308.       return(PARAxNM);
  309.     }
  310.     cmcsb._cmptr = cmdbuf + (cmcsb._cmptr - cmcsb._cmbfp);
  311.     cmcsb._cmcur = cmdbuf + (cmcsb._cmcur - cmcsb._cmbfp);
  312.     cmcsb._cmbfp = cmdbuf;        /* and install it into the CSB */
  313.     
  314.     cmcsb._cmcnt += BSIZ;        /* count it */
  315.     
  316.     if ((cmcsb._cmwbp = (char *)cmrealloc(cmcsb._cmwbp,cmcsb._cmwbc+BSIZ))
  317.     == NULL) {
  318.       return(PARAxNM);
  319.     }
  320.     cmcsb._cmwbc += BSIZ;
  321.   }
  322.  
  323.   if (eof) {                /* done! */
  324.     if (buf != NULL)            /* free previous buffer */
  325.       free(buf);
  326.     
  327.     if ((buf = (char *)malloc(textlen+1)) == NULL) { /* get space */
  328.       return(PARAxNM);
  329.     }
  330.     strncpy(buf,text,textlen);        /* copy text into buffer */
  331.     buf[textlen] = '\0';        /* NULL terminate it */
  332.     for (i = 0; i < textlen; i++)
  333.       buf[i] &= 0x7f;            /* strip those quota characters out. */
  334.     value->_pvpara = buf;        /* point return value at it */
  335.     *parselen = textlen;        /* set the length properly */
  336.     null_actions();            /* restore action block */
  337.     bcopy(cmerjb, ojmp, sizeof (jmp_buf)); /* put back old error handler */
  338.     cmact(oldact);            /* put back the original action table*/
  339.     set = FALSE;            /* initialized...for next time */
  340.     if (cmcsb._cmcol != 0)
  341.     cmxnl();
  342.     free(cmcsb._cmwbp);
  343.     free(cmcsb._cmbfp);
  344.     cmdbuf = NULL;            /* cmdbuf = cmcsb._cmbfp */
  345.     cmcsb = savecsb;            /* restore ccmd's buffers */
  346.     return(CMxOK);            /* return done */
  347.   }
  348.   else
  349.     if (abort) {
  350.       bcopy(ojmp, cmerjb, sizeof (jmp_buf)); /* put back old error handler */
  351.       null_actions();            /* restore action block */
  352.       cmact(oldact);            /* put back the original action table*/
  353.       set = FALSE;            /* uninitialized...for next time */
  354.       value->_pvpara = NULL;
  355.       *parselen = textlen;
  356.       if (cmcsb._cmcol != 0)
  357.       cmxnl();
  358.       free(cmcsb._cmwbp);        /* restore ccmd's buffers */
  359.       cmcsb = savecsb;
  360.       return(CMxOK);
  361.     }
  362.   return(CMxINC);            /* nothing here...return incomplete */
  363. }
  364.  
  365. int break_hndlr();            /* forward declaration */
  366.  
  367. /*
  368.  * scan through para_action table, and install actions in 
  369.  * the real actions tables.
  370.  * terminates in NULL function.
  371.  */
  372. install_actions(actions) para_actions *actions; {
  373.   if (!cmcsb._cmflg & CM_TTY)
  374.     return;
  375.   for(; !(actions->actionfunc==NULL && actions->actionchar==NULL);actions++) {
  376.     paraact[actions->actionchar] = break_hndlr;
  377.     paraact1[actions->actionchar] = actions->actionfunc;
  378.   }
  379. }
  380.  
  381. /*
  382.  * zero out the action tables
  383.  */
  384. null_actions() {
  385.     bzero(paraact, sizeof(paraact));
  386.     bzero(paraact1, sizeof(paraact));
  387. }
  388.  
  389. /*
  390.  * parse completion.
  391.  * does nothing.
  392.  */
  393. PASSEDSTATIC int
  394. paracomplete(text,textlen,fdbp,full,cplt,cpltlen)
  395. char *text,**cplt;
  396. int textlen,full,*cpltlen;
  397. fdb *fdbp;
  398. {
  399.   *cplt = NULL;
  400.   return(CMP_BEL);        /* beep at them */
  401. }
  402.  
  403. /*
  404.  * helpless
  405.  */
  406.  
  407. PASSEDSTATIC int
  408. parahelp(text,textlen,fdbp,cust,lines)
  409. char *text;
  410. int textlen,cust;
  411. fdb *fdbp;
  412. int lines;
  413. {
  414.   return(lines);
  415. }
  416.  
  417. /*
  418.  * Action routines
  419.  */
  420.  
  421. /*
  422.  * eof action.
  423.  */
  424. static char *
  425. eofact(text,modified,ret) char *text; int *modified,*ret; {
  426.   *ret = TRUE;
  427.   *modified = FALSE;
  428.   return(text);
  429. }
  430.  
  431.  
  432. /*
  433.  * redisplay the last screenful of text
  434.  */
  435.  
  436. static char *
  437. redispact(text,modified,ret) char *text; int *modified, *ret;{
  438.   int i, li, co, cols, ov=FALSE;
  439.   char c;
  440.  
  441.   *ret = FALSE;
  442.   *modified = FALSE;
  443.   cmcls();
  444.   li = cmcsb._cmrmx;            /* get number of lines */
  445.   co = cmcsb._cmcmx;            /* and number of columns */
  446.  
  447.   for (i = strlen(text)-1, cols = 0; i >= 0; i--) { /* figure out what */
  448.     c = text[i] & 0x7f;            /*   we can display */
  449.     if (c == '\t')
  450.     cols = ((cols + 8) / 8) * 8;
  451.     else 
  452.     cols++;                /* incr column count */
  453.     if (iscntrl(c) && !isspace(c))    /* control char takes two chars */
  454.       cols++;                /*   to display, count the ^ */
  455.     if (cols > co) {        
  456.       --li;                /* we overflowed the line */
  457.       cols = 0;                /* reset column count */
  458.       ov = TRUE;            /* remember */
  459.     }
  460.     if (c == '\n') {            /* another line */
  461.       cols = 0;                /* reset the column count */
  462.       li--;
  463.       ov = FALSE;
  464.     }
  465.     if (li == 0)            /* can't display any more lines */
  466.       break;
  467.   }
  468.   if (ov && li == 0) {            /* top line doesn't fit on display */
  469.     int p;
  470.     p = i;
  471.     while ((c = text[p] & 0x7f) != '\n' &&  p > 0) /* find beginning of line */
  472.       p--;
  473.     p += ((i - p)/co + 1) * co;
  474.     i = p-1;                /* skip over the \n */
  475.   }
  476.     
  477.   cmcsb._cmcol = 0;
  478.   for(i++; i < cmcsb._cminc; i++) {    /* display the screenful */
  479.     c = text[i] & 0x7f;
  480.     if (iscntrl(c) && !isspace(c)) {
  481.       cmxputc('^');            /* display control chars as */
  482.       cmxputc(c | 0100);        /*   caret and uppercased */
  483.     }
  484.     else cmechx(c);
  485.   }
  486.   return(text);
  487. }
  488.  
  489. /* 
  490.  * insact
  491.  * insert a file at the end of the buffer
  492.  */
  493. static char *
  494. insact(text, modified, ret) char *text; int *modified, *ret; {
  495.                     /* filename fdb */
  496.   static fdb filfdb = { _CMFIL ,FIL_TYPE|FIL_NODIR, NULL, NULL, NULL, NULL,
  497.                 NULL };
  498.                     /* confirm fdb */
  499.   static fdb filfdb1 = { _CMCFM, CM_SDH, &filfdb, NULL, "Confirm to cancel",
  500.              NULL, NULL };
  501.   static fdb cfmfdb = { _CMCFM, 0, NULL, NULL, NULL, NULL, NULL };
  502.   char c;
  503.   char *fname= NULL;
  504.   pval parseval;
  505.   fdb *used;
  506.   int fd;
  507.   struct stat buf;
  508.   char *ntext = NULL;
  509.   int len;
  510.   static int cmdbuf[200];
  511.   static char atmbuf[200],wrkbuf[200];
  512.  
  513.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  514.   cmseter();                /* errors come back to here */
  515.   prompt("Insert file: ");        /* prmpt for a filename */
  516.   cmsetrp();                /* reparses come back here. */
  517.   parse(&filfdb1, &parseval, &used);    /* parse the filename */
  518.   if (used == &filfdb1) {
  519.     cmxputs("...No file inserted\n"); 
  520.     *modified = FALSE;
  521.     return(text);
  522.   }
  523.   if (fname != NULL)            /* free up old filename if */
  524.     free(fname);            /* necessary */
  525.   if ((fname = (char *)malloc(strlen(parseval._pvfil[0])+1)) == NULL) {
  526.     cmxputs("?Out of memory\n");
  527.     return(text);
  528.   }
  529.   strcpy(fname,parseval._pvfil[0]);    /* copy the data */
  530.   parse(&cfmfdb,&parseval,&used);    /* get confirmation */
  531.  
  532.   fd = open(fname,O_RDONLY,0);        /* open up the file */
  533.   if (fd == -1) {            /* couldn't open it... */
  534.     cmxputs("?permission denied\n");    /* complain */
  535.     *modified = FALSE;            /* no modification done */
  536.     return(text);
  537.   }
  538.   stat(fname,&buf);            /* get length of file */
  539.   len = strlen(text) + buf.st_size;
  540.   if ((ntext = (char *)malloc(len+1)) == NULL) { /* make space */
  541.     cmxputs("?Out of memory\n"); 
  542.     return(text);
  543.   }
  544.   strcpy(ntext,text);            /* copy old text */
  545.                     /* and add in the file */  
  546.   len = strlen(text) + read(fd,&ntext[strlen(text)],buf.st_size);
  547.   close(fd);                /* close the file */
  548.   cmxputs("[OK]\n"); 
  549.   ntext[len] = '\0';            /* null terminate */
  550.   *modified = TRUE;            /* we changed things */
  551.   *ret = FALSE;
  552.   return(ntext);            /* return new text */
  553. }
  554.  
  555. /*
  556.  * display the whole buffer
  557.  */
  558. static char *
  559. dispact(text, modified, ret) char *text; int *modified, *ret; {
  560.   if (cmcsb._cmcol != 0)
  561.     cmxnl();
  562.   cmxputs(text); 
  563.   return(text);
  564. }
  565.  
  566. /*
  567.  * invoke the editor on the buffer
  568.  */
  569. static char *
  570. editact(text, modified, ret) char *text; int *modified, *ret; {
  571.   char fname[40];
  572.   char *e,*getenv();
  573.   int fd;
  574.   char buf[100];
  575.   static char *ntext=NULL;
  576.   char *mktemp();
  577.   struct stat sbuf;
  578.   int len,i;
  579.  
  580.   cmxputs("Invoking editor...\n");    /* announce our intentions */
  581.   e = getenv("EDITOR");            /* check editor variable */
  582.   if (e == NULL || *e == '\0')        /* no editor variable? */
  583.     e = DEF_EDITOR;            /* use default editor */
  584.   
  585. #if unix
  586.     strcpy(fname,"/tmp/cmdXXXXXX");    /* get a file name */
  587.     mktemp(fname);
  588. #else
  589.   strcpy(fname,mktemp(getenv("TMP"),"cmd")); /* get a file name */  
  590. #endif
  591.   fd = open(fname,O_WRONLY|O_CREAT,0700); /* open the tmp file */
  592.   if (fd < 0) {
  593.     cmxputs("?Could not create temporary file.\n"); 
  594.     perror(fname);
  595.     return(text);
  596.   }
  597.   if (write(fd,text,strlen(text)) != strlen(text)) {
  598.     cmxputs("?Could not write temporary file.\n"); 
  599.     perror(fname);
  600.     close(fd);
  601.     unlink(fname);
  602.     return(text);
  603.   }
  604.   close(fd);
  605.   strcpy(buf,e);
  606. #ifndef MSDOS
  607.   strcat(buf," +1000000");
  608. #endif
  609.   strcat(buf," ");
  610.   strcat(buf,fname);
  611.   cmtend();                /* put the tty back */
  612.   if (system(buf) != 0) {        /* execute it */
  613.     cmtset();
  614.     cmxputs("?Edit failed.\n");        /* failure? */
  615.     perror(fname);
  616.     unlink(fname);            /* throw up hands in disgust */
  617.     return(text);
  618.   }
  619.   cmtset();
  620.   if (stat(fname,&sbuf) == -1) {    /* get length of file */
  621.     cmxputs("?Temporary file disappeared\n"); 
  622.     return(text);
  623.   }
  624.   len = sbuf.st_size;
  625.   fd = open(fname,O_RDONLY,0);        /* open the file again */
  626.   if (fd < 0) {
  627.     cmxputs("?Could not open temporary file for read.\n"); 
  628.     perror(fname);
  629.     unlink(fname);
  630.     return(text);
  631.   }
  632.   if (ntext != NULL)            /* free up last buffer */
  633.     free(ntext);
  634.   if ((ntext = (char *)malloc(len+1)) == NULL) { /* make space */
  635.     cmxputs("?Out of memory\n"); 
  636.     return(text);
  637.   }
  638. #ifdef MSDOS
  639.   if ((i = read(fd,ntext,len)) < 0)  /* read in the file */
  640. #else
  641.   if ((i = read(fd,ntext,len)) != len) /* read in the file */
  642. #endif
  643.   {
  644.     cmxputs("?Could not read temporary file.\n"); 
  645.     perror(fname);
  646.     unlink(fname);
  647.     return(text);
  648.   }
  649.   close(fd);                /* close the file */
  650.   cmcls();
  651.   ntext[len] = '\0';            /* null terminate */
  652.   *modified = TRUE;
  653.   unlink(fname);
  654.   cmxputs("...Back again\n"); 
  655.   return(ntext);
  656. }
  657.  
  658. /*
  659.  * abort the current buffer
  660.  */
  661. static char *
  662. abortact(text, modified, ret) char *text; int *modified, *ret; {
  663.   static fdb cfmfdb = { _CMCFM, CM_SDH, NULL, NULL, "Carriage Return to abort",
  664.             NULL, NULL };
  665.   char cmdbuf[200],atmbuf[200],wrkbuf[200];
  666.   pval parseval;
  667.   fdb *used;
  668.  
  669.   cmcsb._cmerr = CMxOK;
  670.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  671.   cmseter();                /* errors come back to here */
  672.   if (cmcsb._cmerr == CFMxNOC) {
  673.     cmcsb._cmflg &= ~CM_ACT;        /* no pending actions now */
  674.     return(text);
  675.   }
  676.   prompt("[Confirm to Abort]");        /* prompt for a confirm */
  677.   cmsetrp();                /* reparses come back here. */
  678.   parse(&cfmfdb, &parseval, &used);    /* parse the confirm */
  679.   abort = TRUE;
  680.   return(text);
  681. }
  682.  
  683. /*
  684.  * just make us wake up on eol, to check on buffer space...
  685.  */
  686.  
  687. static char *
  688. eolact(text, modified, ret) char *text; int *modified, *ret; {
  689.   static char *ntext=NULL;
  690.   
  691.   cmechx('\n');                /* do it again */
  692.   *modified = TRUE;            /* we modified it */
  693.   *ret = FALSE;                /* don't return yet */
  694.   strcat(text,"\n");
  695.   return(ntext);
  696. }  
  697.  
  698.  
  699. static char *
  700. executeact(text, modified, ret) 
  701. char *text; 
  702. int *modified, *ret; 
  703. {
  704.   char fname[40];
  705.   char *getenv();
  706.   char *output;
  707.   char *cmd, *parse_cmdline();
  708.   struct stat sbuf;
  709.   int fd;
  710.   int len;
  711.   int total;
  712.  
  713.   if ((cmd = parse_cmdline("Command: ")) == NULL) {
  714.     *modified = FALSE;
  715.     return (text);
  716.   }
  717.  
  718. #if unix
  719.   strcpy(fname,"/tmp/cmdXXXXXX");    /* get a file name */
  720.   mktemp(fname);
  721. #else
  722.   strcpy(fname,mktemp(getenv("TMP"),"cmd")); /* get a file name */  
  723. #endif
  724.  
  725.   cmd = (char *) cmrealloc (cmd, strlen(cmd)+strlen(fname)+strlen(" > ")+1);
  726.   strcat (cmd, " > ");
  727.   strcat (cmd, fname);
  728.   
  729.   cmsystem (cmd);
  730.   free (cmd);
  731.  
  732.   if (stat(fname, &sbuf) == -1) {
  733.     cmxputs ("?Temporary file missing\n");
  734.     return (text);
  735.   }
  736.   len = sbuf.st_size;
  737.   total = strlen(text)+len;
  738.   if ((output = (char *) malloc (total+1)) == NULL) {
  739.     cmxputs ("?Out of memory\n");
  740.     *modified = FALSE;
  741.     return (text);
  742.   }
  743.   strcpy (output, text);
  744.   if ((fd = open(fname, O_RDONLY,0)) < 0) {
  745.     cmxputs("?Could not open temporary file for read.\n"); 
  746.     perror(fname);
  747.     unlink(fname);
  748.     free (output);
  749.     return(text);
  750.   }
  751. #ifdef MSDOS
  752.   if (read(fd,output+strlen(text),len) < 0)
  753. #else
  754.   if (read(fd,output+strlen(text),len) != len)
  755. #endif
  756.   {
  757.     cmxputs("?Could not read temporary file.\n"); 
  758.     perror(fname);
  759.     unlink(fname);
  760.     free (output);
  761.     return(text);
  762.   }
  763.   close(fd);                /* close the file */
  764.   output[total] = '\0';
  765.   unlink (fname);
  766.   *modified = TRUE;
  767.   *ret = FALSE;
  768.   cmxputs("[Done]\n");
  769.   return (output);
  770. }
  771.  
  772.  
  773. static brktab fieldbrk = {
  774.   {                    /* print chars except ?, space, tab */
  775.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01,
  776.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  777.   },
  778.   {                    /* print chars except ?, space, tab */
  779.     0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x01,
  780.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  781.   }
  782. };
  783. static filblk efilblk = { NULL, NULL, NULL };
  784. static fdb efilefdb = { _CMFIL, FIL_OLD|FIL_EXEC|CM_SDH, NULL, 
  785.               (pdat) &efilblk, NULL, NULL, NULL };
  786. static fdb filefdb = { _CMFIL, CM_SDH, NULL, 
  787.              (pdat) NULL, NULL, NULL, NULL };
  788. static fdb fieldfdb = { _CMFLD, CM_SDH, NULL, NULL, NULL, NULL, &fieldbrk };
  789. static fdb conffdb = { _CMCFM, CM_SDH, NULL, NULL, 
  790.              "command line", NULL, NULL };
  791. static char **path = NULL;
  792.  
  793. static char *
  794. parse_cmdline (p) char *p; {
  795.   char *cmd;
  796.   char *cmdend;
  797.   pval parseval;
  798.   fdb *used;
  799.   char **get_path();
  800.   char *parse_cmdline1();
  801.   static int cmdbuf[200];
  802.   static char atmbuf[200],wrkbuf[200];
  803.  
  804.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200);
  805.  
  806.   path = get_path();
  807.   efilblk.pathv = path;
  808.  
  809.   cmseter();
  810.   prompt (p);
  811.   cmsetrp();
  812.   parse (fdbchn(&conffdb, &efilefdb, &filefdb, &fieldfdb, NULL), 
  813.      &parseval, &used);
  814.  
  815.   if (used == &conffdb) {
  816.     cmxputs ("Aborting\n");
  817.     return (NULL);
  818.   }
  819.  
  820.   if (used == &efilefdb || used == &filefdb) {
  821.     cmd = (char *) malloc (strlen (parseval._pvfil[0]) + 1);
  822.     strcpy (cmd, parseval._pvfil[0]);
  823.   }
  824.   else if (used == &fieldfdb) {
  825.     cmd = (char *) malloc (strlen (parseval._pvstr) + 1);
  826.     strcpy (cmd, parseval._pvstr);
  827.   }
  828.   
  829.   if (cmdend = parse_cmdline1()) {
  830.       cmd = (char *) realloc (cmd, strlen (cmd)+strlen(cmdend)+2);
  831.       strcat (cmd, " ");
  832.       strcat (cmd, cmdend);
  833.       free (cmdend);
  834.   }
  835.   return (cmd);
  836. }
  837.  
  838. static char *
  839. parse_cmdline1 () {
  840.   char *cmd;
  841.   char *cmdend;
  842.   pval parseval;
  843.   fdb *used;
  844.   
  845.   parse (fdbchn(&conffdb, &filefdb, &fieldfdb, NULL), 
  846.      &parseval, &used);
  847.  
  848.   if (used == &conffdb)
  849.     return (NULL);
  850.  
  851.   if (used == &filefdb) {
  852.     cmd = (char *) malloc (strlen (parseval._pvfil[0]) + 1);
  853.     strcpy (cmd, parseval._pvfil[0]);
  854.   }
  855.   else if (used == &fieldfdb) {
  856.     cmd = (char *) malloc (strlen (parseval._pvstr) + 1);
  857.     strcpy (cmd, parseval._pvstr);
  858.   }
  859.   
  860.   if (cmdend = parse_cmdline1()) {
  861.       cmd = (char *) realloc (cmd, strlen (cmd)+strlen(cmdend)+2);
  862.       strcat (cmd, " ");
  863.       strcat (cmd, cmdend);
  864.       free (cmdend);
  865.   }
  866.   return (cmd);
  867. }
  868.  
  869.  
  870. static char **
  871. get_path() {
  872.   char **path = NULL;
  873.   int num = 0;
  874.   char *PATH, *cp, *getenv();
  875.   char *p, *index();
  876.  
  877.   if ((PATH = getenv ("PATH")) == NULL)
  878.     return (NULL);
  879.   else {
  880.     cp = (char *) malloc (strlen(PATH)+1); /* space for private copy */
  881.     strcpy (cp, PATH);
  882.   }
  883.  
  884.   while ((p = index (cp, ':')) != NULL) { /* while we have dirs */
  885.     *p = '\0';                /* tie of with a NULL */
  886.     if (strlen(cp) != 0) {
  887.       if (num == 0)             /* first path */
  888.     path = (char **) malloc (sizeof (char *)); /* space for 1 */
  889.       else                /* need more space */
  890.     path = (char **) realloc (path, (num+1)*sizeof(char *));
  891.       path[num] = (char *) malloc (strlen(cp)+1); /* space for path */
  892.       strcpy (path[num], cp);        /* copy path into array */
  893.       num++;                /* increment count */
  894.     }
  895.     cp = ++p;                /* move on */
  896.   }
  897.   /* the last path element */
  898.   path = (char **) realloc (path, (num+2)*sizeof(char *));
  899.   path[num] = (char *) malloc (strlen(cp)+1);
  900.   strcpy (path[num], cp);
  901.   num++;
  902.   path[num] = NULL;            /* tie off with null path */
  903.   return (path);
  904. }
  905.  
  906. static char *
  907. autowrapspace (text, modified, ret)
  908. char *text;
  909. int *modified, *ret;
  910. {
  911.   return (autowrapact (text, modified, ret, ' '));
  912. }
  913.  
  914. static char *
  915. autowrapnl (text, modified, ret)
  916. char *text;
  917. int *modified, *ret;
  918. {
  919.   return (autowrapact (text, modified, ret, '\n'));
  920. }
  921.  
  922.  
  923. static char *
  924. autowraptab (text, modified, ret)
  925. char *text;
  926. int *modified, *ret;
  927. {
  928.   return (autowrapact (text, modified, ret, '\t'));
  929. }
  930.  
  931. static char *
  932. autowrapact(text, modified, ret, c)
  933. char *text;
  934. int *modified, *ret;
  935. char c;
  936. {
  937.     int i,j;
  938.     extern csb cmcsb;
  939.     int wordbeg, spacebeg;
  940.     int maxcol;
  941.  
  942.     maxcol = cmcsb._cmwrp >= 0 ? cmcsb._cmwrp : cmcsb._cmcmx + cmcsb._cmwrp;
  943.     if (cmcsb._cmwrp != 0 && lastlinelen(text) >= maxcol) {
  944.     for(i = strlen(text)-1; i >= 0; i--) {
  945.         if (text[i] == ' ' || text[i] == '\t') {
  946.         wordbeg = i+1;
  947.         i--;
  948. #ifdef undef
  949.         while((text[i] == ' ' || text[i] == '\t') && i >= 0) i--;
  950. #endif
  951.         spacebeg = i+1;
  952.         break;
  953.         }
  954.         else {
  955.         if (text[i] == '\n' || i == 0 ||
  956.             strlen(text) - i >= maxcol) {
  957.             strcat(text,"\n");
  958.             cmechx('\n');
  959.             *ret = FALSE;
  960.             *modified = TRUE;
  961.             return(text);
  962.         }
  963.         }
  964.     }
  965.     if (cmcsb._cmcol != strlen(text) - wordbeg) {
  966.         delcurrent(strlen(text) - wordbeg);
  967.         cmechx('\n');
  968.         cmxputs(&text[wordbeg]);
  969.     }
  970.     cmechx(c);
  971.     text[spacebeg] = '\n';
  972.     text[strlen(text)+1] = '\0';
  973.     text[strlen(text)] = c;
  974.     *ret = FALSE;
  975.     *modified = TRUE;
  976.     return(text);
  977.     }
  978.     text[strlen(text)+1] = '\0';
  979.     text[strlen(text)] = c;
  980.     cmechx(c);
  981.     *ret = FALSE;
  982.     *modified = TRUE;
  983.     return(text);
  984. }
  985.  
  986. static
  987. lastlinelen(str) 
  988. char *str;
  989. {
  990.     register int i,j=strlen(str);
  991.     for(i = j - 1; i >= 0; i--)
  992.     if (str[i] == '\n')
  993.         return(tablen(str + i + 1));
  994.     return(tablen(str));
  995. }
  996.  
  997. /* 
  998.  * calculate length of a line, even if there are tabs in it.
  999.  */
  1000.  
  1001. static int
  1002. tablen(str) 
  1003. char *str;
  1004. {
  1005.     register char *cp = str;
  1006.     register int len=0;
  1007.     while(*cp) {
  1008.     if (*cp == '\t') {
  1009.         len = ((len + 8) / 8) * 8;
  1010.     }
  1011.     else
  1012.         len++;
  1013.     cp++;
  1014.     }
  1015.     return(len);
  1016. }
  1017.         
  1018.  
  1019. static char *
  1020. filteract (text, modified, ret) 
  1021. char *text; 
  1022. int *modified, *ret; 
  1023. {
  1024.   char fname1[40];
  1025.   char fname2[40];
  1026.   char *getenv();
  1027.   char *output;
  1028.   char *cmd, *parse_cmdline();
  1029.   struct stat sbuf;
  1030.   int fd;
  1031.   int len;
  1032.   int total;
  1033.   FILE *pp;
  1034.  
  1035.   if ((cmd = parse_cmdline("Filter: ")) == NULL) {
  1036.     *modified = FALSE;
  1037.     return (text);
  1038.   }
  1039.  
  1040. #if unix
  1041.   strcpy(fname1,"/tmp/cmd1XXXXXX");    /* get a file name */
  1042.   mktemp(fname1);
  1043.   strcpy(fname2,"/tmp/cmd2XXXXXX");    /* get a file name */
  1044.   mktemp(fname2);
  1045. #else
  1046.   strcpy(fname1,mktemp(getenv("TMP"),"cmd1")); /* get a file name */  
  1047.   strcpy(fname2,mktemp(getenv("TMP"),"cmd2")); /* get a file name */  
  1048. #endif
  1049.  
  1050.   cmd = (char *) realloc (cmd, strlen(cmd)+strlen(fname1)+strlen(" < ")+
  1051.               strlen(" > ") + strlen(fname2) +1);
  1052.   strcat (cmd, " < ");
  1053.   strcat (cmd, fname1);
  1054.   strcat (cmd, " > ");
  1055.   strcat (cmd, fname2);
  1056.   if ((pp = fopen (fname1, "w")) == NULL) {
  1057.     cmxputs ("Could not open temp file to execute command\n");
  1058.     *modified = FALSE;
  1059.     free (cmd);
  1060.     return (text);
  1061.   }
  1062.   fprintf (pp, "%s", text);
  1063.   fclose (pp);
  1064.   system(cmd);
  1065.   free (cmd);
  1066.   unlink(fname1);
  1067.  
  1068.   if (stat(fname2, &sbuf) == -1) {
  1069.     cmxputs ("?Temporary file missing\n");
  1070.     return (text);
  1071.   }
  1072.   len = sbuf.st_size;
  1073.   if ((output = (char *) malloc (len+1)) == NULL) {
  1074.     cmxputs ("?Out of memory\n");
  1075.     *modified = FALSE;
  1076.     unlink(fname2);
  1077.     return (text);
  1078.   }
  1079.   if ((fd = open(fname2, O_RDONLY,0)) < 0) {
  1080.     cmxputs("?Could not open temporary file for read.\n"); 
  1081.     perror(fname2);
  1082.     unlink(fname2);
  1083.     free (output);
  1084.     return(text);
  1085.   }
  1086. #ifdef MSDOS
  1087.   if (read(fd,output,len) < 0)
  1088. #else
  1089.   if (read(fd,output,len) != len)
  1090. #endif
  1091.   {
  1092.     cmxputs("?Could not read temporary file.\n"); 
  1093.     perror(fname2);
  1094.     unlink(fname2);
  1095.     free (output);
  1096.     return(text);
  1097.   }
  1098.   close(fd);                /* close the file */
  1099.   output[len] = '\0';
  1100.   unlink (fname2);
  1101.   *modified = TRUE;
  1102.   *ret = FALSE;
  1103.   cmxputs("[Done]\n");
  1104.   return (output);
  1105. }
  1106.  
  1107.  
  1108.    
  1109. #ifdef MSDOS
  1110. static char *
  1111. mktemp(dir,prefix) char *dir, *prefix; {
  1112.   static int inited=FALSE;
  1113.   int x;
  1114.   char name[100];
  1115.   struct stat sbuf;
  1116.   if (!inited++)
  1117.     srand(1);
  1118.   x = rand();
  1119.   if (dir == NULL)
  1120.     dir = "./";
  1121.   strcpy(name,dir);
  1122.   strcat(name,"\\");
  1123.   strcat(name,prefix);
  1124.   strcat(name,atoi(x));
  1125.   if (stat(name,&sbuf) == 0)
  1126.     return(mktemp(dir,prefix));
  1127.   return(name);
  1128. }
  1129. #endif /*  MSDOS */
  1130.  
  1131. cm_para_abort() {
  1132.   abort = TRUE;
  1133. }
  1134.  
  1135. cm_para_eof() {
  1136.   eof = TRUE;
  1137. }
  1138.  
  1139.  
  1140. /*
  1141.  * delete to beginning of line.   mostly taken from begact() in stdact.c
  1142.  */
  1143. static char *
  1144. begact(text,modified,ret) char *text; int *modified, *ret; {
  1145.   char *cp;                /* pointer to deletion site */
  1146.   int cc;                /* character under examination */
  1147.   char c;
  1148.   int eralen;                /* # of chars erased */
  1149.  
  1150.   *modified = FALSE;
  1151.   *ret = FALSE;
  1152.   if (strlen(text) == 0) return(text);
  1153.   cp = text + strlen(text) - 1;     /* point to end of buffer */
  1154.   while (cp-- != text) {        /* search for nonhidden newline */
  1155.     if (*cp == NEWLINE)            /* newline? */
  1156.       break;                /* yup, stop looking */
  1157.   }
  1158.   cp++;                    /* point to char after break */
  1159.   eralen = (text - cp) + strlen(text); /* get # of chars erased */
  1160.   if (eralen <= 0) return(text);
  1161.   if (cmcsb._cmflg2 & CM_CRT)
  1162.     delcurrent(eralen);            /* erase the characters */
  1163.   else {
  1164.     cmxputs("^U");            /* signal line kill on hardcopy */
  1165.     cmxnl();                /* move to a new line */
  1166.   }
  1167.   *modified = TRUE;
  1168.   text[strlen(text)-eralen] = '\0';
  1169.   return(text);
  1170. }
  1171.  
  1172. /*
  1173.  * wrdact:
  1174.  * delete backwards one word.  If at the beginning of a line, 
  1175.  * and previous line is empty, just deletes to beginning of
  1176.  * previous line.
  1177.  */
  1178. static char *
  1179. wrdact(text,modified,ret) char *text; int *modified, *ret; {
  1180.   char *cp;                /* pointer to deletion site */
  1181.   int cc;                /* character under examination */
  1182.   char c;
  1183.   int eralen;                /* # of chars erased */
  1184.  
  1185.   *modified = FALSE;
  1186.   *ret = FALSE;
  1187.   if (strlen(text) == 0) {
  1188.     beep();
  1189.     return(text);
  1190.   }
  1191.   cp = text + strlen(text) - 1;     /* point to end of buffer */
  1192.   while (cp-- != text) {        /* search for a character which is */
  1193.     c = *cp;
  1194.     if ((c < '0') ||            /* not a letter or digit? */
  1195.      ((c > '9') && (c < 'A')) ||
  1196.      ((c > 'Z') && (c < 'a')) ||
  1197.      (c > 'z')
  1198.     )
  1199.       break;                /* yup, stop looking */
  1200.   }
  1201.   cp++;                    /* point to char after break */
  1202.   eralen = (text - cp) + cmcsb._cminc; /* get # of chars erased */
  1203.   if (eralen <= 0) return(text);
  1204.   if (cmcsb._cmflg2 & CM_CRT)
  1205.     delcurrent(eralen);            /* erase the characters */
  1206.   else {
  1207.     cmxputs("^W");            /* signal line kill on hardcopy */
  1208.   }
  1209.   *modified = TRUE;
  1210.   text[strlen(text)-eralen] = '\0';    /* modify in place */
  1211.   return(text);
  1212. }
  1213.  
  1214. /* fixact
  1215. **
  1216. ** Purpose:
  1217. **   Refresh the display of the current line of text, back to the
  1218. **   last unhidden newline character.  If the last character in the
  1219. **   buffer is a newline, the previous line is refreshed.
  1220. **/
  1221.  
  1222. static char *
  1223. fixact(text,modified,ret) char *text; int *modified, *ret; {
  1224.   char *cp;                /* pointer into buffer */
  1225.   int cc;                /* character under examination */
  1226.   char c;
  1227.   int reflen;                /* # of chars refreshed */
  1228.  
  1229.   *ret = FALSE;
  1230.   *modified = FALSE;
  1231.   if (strlen(text) == 0) return(text);
  1232.   cp = text + strlen(text) -1;        /* point to end of buffer */
  1233.  
  1234.   while (cp-- != text) {        /* search for nonhidden newline */
  1235.     if (*cp == NEWLINE)            /* newline? */
  1236.       break;                /* yup, stop looking */
  1237.   }
  1238.   cp++;                    /* point to char after break */
  1239.  
  1240.   reflen = (text - cp) + strlen(text); /* get # of chars to refresh */
  1241.  
  1242.   if (cmcsb._cmflg2 & CM_CRT)
  1243.     delcurrent(reflen);            /* erase the characters from screen */
  1244.   else {
  1245.     cmxputs("^R");            /* signal line kill on hardcopy */
  1246.     cmxnl();                /* move to a new line */
  1247.   }
  1248.   while (reflen-- > 0) {        /* retype buffer contents */
  1249.     c = (cc = *cp++) & CC_CHR;        /* get next char */
  1250.     cmechx(c);                /* do it again */
  1251.   }
  1252.   return(text);
  1253. }
  1254.  
  1255. /* quoact
  1256. **
  1257. ** Purpose:
  1258. **   Enter the next character into the buffer with its quote flag
  1259. **   turned on, so it will be treated as a normal character regardless
  1260. **   of any handling it would normally receive.
  1261. **/
  1262. static char *
  1263. quoact(text,modified,ret) char *text; int *modified, *ret; {
  1264.   int c;                /* quoted character */
  1265.   
  1266.   cmgetc(&c,cmcsb._cmij);        /* get another character */
  1267.   cmechx(c);                /* do it again */
  1268.   *modified = TRUE;            /* we modified it */
  1269.   *ret = FALSE;                /* don't return yet */
  1270.   text[strlen(text)+1] = '\0';  
  1271.   text[strlen(text)] = c;   /* | CC_QUO; */
  1272.   return(text);
  1273. }
  1274.  
  1275.  
  1276. /* delact
  1277. **
  1278. ** Purpose:
  1279. **   Erase back to and including the last non-hidden character in the
  1280. **   command buffer.
  1281. **/
  1282. static char *
  1283. delact(text,modified,ret) char *text; int *modified, *ret; {
  1284.   char *cp;                /* pointer to deletion site */
  1285.   int cc;                /* character under examination */
  1286.   char c;
  1287.   int eralen;                /* # of chars erased */
  1288.  
  1289.   *modified = FALSE;
  1290.   *ret = FALSE;
  1291.   if (strlen(text) == 0) {
  1292.     beep();
  1293.     return(text);
  1294.   }
  1295.   eralen = 1;
  1296.   if (cmcsb._cmflg2 & CM_CRT)
  1297.     delcurrent(eralen);            /* erase the characters */
  1298.   else {
  1299.     cmxputs("\\");            /* signal line kill on hardcopy */
  1300.   }
  1301.   *modified = TRUE;
  1302.   text[strlen(text)-eralen] = '\0';    /* modify in place */
  1303.   return(text);
  1304. }
  1305.  
  1306.  
  1307.  
  1308. /*
  1309.  * paragraph action char handler.
  1310.  * calls user defined handlers, fixes up text, and returns
  1311.  * CMxGO
  1312.  * Must perform context switch on ccmd's buffers, and maintain enough
  1313.  * buffer space for the current buffer
  1314.  */
  1315. break_hndlr(fdblist,brk,deferred) 
  1316. fdb *fdblist;
  1317. char brk;
  1318. int deferred;
  1319. {
  1320.   csb savecsb;
  1321.   jmp_buf rpbuf, erbuf;
  1322.   int col;
  1323.   int len, ret, modified;
  1324.   char *text, *ntext;
  1325.   int i;
  1326.  
  1327.   if (paraact[brk] == NULL) {
  1328.     cmsti1(brk,0);
  1329.     return(CMxGO);
  1330.   }
  1331.  
  1332.   if ((text = (char *)malloc(cmcsb._cminc+3)) == NULL) { /* space for buffer */
  1333.     return(PARAxNM);            /* and a few added chars */
  1334.   }
  1335.  
  1336.   for (i = 0; i < cmcsb._cminc; i++) {
  1337.     text[i] = cmcsb._cmptr[i] & 0x7f;
  1338.     if (text[i] == '\0') text[i] |= 0x80;
  1339.   }
  1340.   text[cmcsb._cminc] = '\0';
  1341.  
  1342.   savecsb = cmcsb;            /* save old state */
  1343.   cmcsb._cmrty = "";            /* no prompt here. */
  1344.   cmact(oldact);
  1345.   bcopy(cmrpjb,rpbuf,sizeof(jmp_buf));
  1346.   bcopy(cmerjb,erbuf,sizeof(jmp_buf));
  1347.  
  1348.   modified = ret = FALSE;        /* default values */
  1349.  
  1350.   if (paraact1[brk] != NULL) {        /* call handler with text */
  1351.     ntext = (*paraact1[brk])(text,&modified,&ret);
  1352.   }
  1353.   else
  1354.       ntext = NULL;
  1355.   col = cmcsb._cmcol;            /* need to remember where we were on */
  1356.                     /* the screen.   this is independent */
  1357.                     /* of the rest of the csb state. */
  1358.  
  1359.   cmcsb = savecsb;            /* restore old state */
  1360.   cmcsb._cmcol = col;
  1361.   bcopy(rpbuf,cmrpjb,sizeof(jmp_buf));
  1362.   bcopy(erbuf,cmerjb,sizeof(jmp_buf));
  1363.   cmact(paraact);
  1364.  
  1365.   if (text != ntext) {            /* new buffer. */
  1366.     free(text);                /* get rid of old one */
  1367.     text = NULL;
  1368.   }
  1369.  
  1370.   if (modified) {
  1371.     if (strlen(ntext) >= 
  1372.     cmcsb._cmcnt + cmcsb._cminc) { /* out of space? */
  1373.                     /* then get some more space */
  1374.       cmcsb._cmcnt = strlen(ntext);
  1375.       cmcsb._cminc = 0;
  1376.       if ((cmdbuf = 
  1377.     (int *)cmrealloc(cmdbuf, (cmcsb._cmcnt + BSIZ) * sizeof(int)))==NULL) {
  1378.     return(PARAxNM);
  1379.       }
  1380.       cmcsb._cmcnt += BSIZ;        /* count it */
  1381.       if ((cmcsb._cmwbp = 
  1382.        (char *)cmrealloc(cmcsb._cmwbp,
  1383.                  sizeof(*cmcsb._cmwbp)*cmcsb._cmcnt))==NULL){
  1384.     return(PARAxNM);
  1385.       }
  1386.       cmcsb._cmwbc = cmcsb._cmcnt;    /* and install it into the CSB */
  1387.       cmcsb._cmptr = cmdbuf;
  1388.       cmcsb._cmbfp = cmdbuf;
  1389.       cmcsb._cmcur = cmdbuf;
  1390.     }
  1391.     else {
  1392.       cmcsb._cmptr = cmcsb._cmcur = cmcsb._cmbfp;
  1393.       cmcsb._cmcnt += cmcsb._cminc;
  1394.       cmcsb._cminc = 0;
  1395.       cmcsb._cmwbc = cmcsb._cmcnt;
  1396.     }
  1397.     len = strlen(ntext);
  1398.     for (i = 0; i < len; i++)        /* unquote everything */
  1399.       ntext[i] &= 0x7f;
  1400.     cmsti(ntext,CC_NEC|CC_HID|CC_QUO);        /* insert text into buffer */
  1401.     for (i = 0; i < cmcsb._cminc; i++)
  1402.       cmcsb._cmptr[i] &= ~(CC_HID|CC_NEC); /* mark it unhidden */
  1403.   }
  1404.   eof = ret;
  1405.   if (ntext)
  1406.     free(ntext);
  1407.   return(CMxGO);
  1408. }
  1409.  
  1410. char *
  1411. self_ins_nl(text, modified, ret)
  1412. char *text;
  1413. int *modified, *ret;
  1414. {
  1415.   strcat(text,"\n");
  1416.   cmechx('\n');
  1417.   *modified = TRUE;
  1418.   return(text);
  1419. }
  1420.  
  1421. install_nl_action()
  1422. {
  1423.   paraact['\n'] = break_hndlr;
  1424.   paraact1['\n'] = self_ins_nl;
  1425. }
  1426.  
  1427.